package com.ocean.product.service.impl;

import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.UUID;

import com.ocean.common.config.RuoYiConfig;
import com.ocean.common.constant.Constants;
import com.ocean.common.core.domain.entity.SysDictData;
import com.ocean.common.utils.*;
import com.ocean.common.utils.file.FileUploadUtils;
import com.ocean.common.utils.file.FileUtils;
import com.ocean.common.utils.swf.SwfToolsUtil;
import com.ocean.common.utils.uuid.IdUtils;
import com.ocean.system.service.ISysDictDataService;
import org.apache.commons.io.FilenameUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ocean.product.mapper.TSDocumentMapper;
import com.ocean.product.domain.TSDocument;
import com.ocean.product.service.ITSDocumentService;
import com.ocean.common.core.text.Convert;
import org.springframework.util.CollectionUtils;
import org.springframework.util.FileCopyUtils;
import org.springframework.web.multipart.MultipartFile;

/**
 * 文档下载,新闻,法规Service业务层处理
 *
 * @author ocean
 * @date 2023-02-06
 */
@Service
public class TSDocumentServiceImpl implements ITSDocumentService
{
    @Autowired
    private TSDocumentMapper tSDocumentMapper;
    @Autowired
    private ISysDictDataService dictDataService;

    /**
     * 查询文档下载,新闻,法规
     *
     * @param id 文档下载,新闻,法规主键
     * @return 文档下载,新闻,法规
     */
    @Override
    public TSDocument selectTSDocumentById(String id)
    {
        TSDocument tsDocument = tSDocumentMapper.selectTSDocumentById(id);
        String realPath = tsDocument.getRealpath();
        String path = StringUtils.substring(realPath,0,realPath.lastIndexOf("/")+1);
        tsDocument.setFilePath(path+tsDocument.getAttachmenttitle()+"."+tsDocument.getExtend());
        return tsDocument;
    }

    /**
     * 查询文档下载,新闻,法规列表
     *
     * @param tSDocument 文档下载,新闻,法规
     * @return 文档下载,新闻,法规
     */
    @Override
    public List<TSDocument> selectTSDocumentList(TSDocument tSDocument)
    {
        return tSDocumentMapper.selectTSDocumentList(tSDocument);
    }

    /**
     * 新增文档下载,新闻,法规
     *
     * @param tSDocument 文档下载,新闻,法规
     * @return 结果
     */
    @Override
    public int insertTSDocument(TSDocument tSDocument)
    {
        return tSDocumentMapper.insertTSDocument(tSDocument);
    }

    /**
     * 修改文档下载,新闻,法规
     *
     * @param tSDocument 文档下载,新闻,法规
     * @return 结果
     */
    @Override
    public int updateTSDocument(TSDocument tSDocument)
    {
        return tSDocumentMapper.updateTSDocument(tSDocument);
    }

    /**
     * 批量删除文档下载,新闻,法规
     *
     * @param ids 需要删除的文档下载,新闻,法规主键
     * @return 结果
     */
    @Override
    public int deleteTSDocumentByIds(String ids)
    {
        return tSDocumentMapper.deleteTSDocumentByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除文档下载,新闻,法规信息
     *
     * @param id 文档下载,新闻,法规主键
     * @return 结果
     */
    @Override
    public int deleteTSDocumentById(String id)
    {
        return tSDocumentMapper.deleteTSDocumentById(id);
    }

    /**
     * 文件录入——保存
     * @param document
     * @return
     */
    @Override
    public int upload(TSDocument document) {
        //设置id
        String id = IdUtils.simpleUUID();
        document.setId(id);document.setID(id);
        //设置字典对应id
        List<SysDictData> fiel_type = DictUtils.getDictCache("fiel_type");
        if (!CollectionUtils.isEmpty(fiel_type))
            document.setTypeid(fiel_type.get(0).getDictCode());
        document.setSubclassname(MyClassLoader.getPackPath(document));
        document.setCreatedate(DateUtils.getNowDate());
        MultipartFile file = document.getFile();
        String fileName = "";
        String extension = FileUploadUtils.getExtension(file);
        try {
            fileName = FileUploadUtils.upload(file);
            fileName = RuoYiConfig.getProfile()+fileName.replace(Constants.RESOURCE_PREFIX + "/","");
            document.setAttachmentcontent(StreamUtils.InputStreamTOByte(file.getInputStream()));
        } catch (IOException e) {
            e.printStackTrace();
            return -1;
        }
        //保存路径
        String savePath = StringUtils.substring(fileName,0,fileName.lastIndexOf("/")+1);
        //自定义文件名称
        String attachFileName = DateUtils.dateTimeNow()+ StringUtils.random(8);
        document.setAttachmenttitle(FileUtils.getNameNotSuffix(fileName));
        document.setExtend(extension);
        String realPath = savePath + attachFileName + "." + extension;
        document.setRealpath(realPath);
        document.setSwfpath(savePath + attachFileName + ".swf");
        try {
            document.setAttachmentcontent(StreamUtils.InputStreamTOByte(file.getInputStream()));
            // 文件拷贝到指定硬盘目录
            FileCopyUtils.copy(file.getBytes(), new File(fileName));
        } catch (IOException e) {
            e.printStackTrace();
        }
        // 转SWF
        SwfToolsUtil.convert2SWF(fileName);
        return tSDocumentMapper.insertTSDocument(document);
    }
}
